
#import "TorrentDescAniDBViewController.h"

@implementation TorrentDescAniDBViewController

//
// web view
//
@synthesize webView															= _webView;

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// desc or anidb
//
@synthesize showDescription													= _showDescription;

//
// dealloc
//
- (void)dealloc
{
	self.webView															= nil;
	self.torrentInfo														= nil;
	
	[super dealloc];
}

//
// setup web view content
//
- (void)setupWebViewContent
{
	NSString* content														= self.showDescription ? self.torrentInfo.torrentDescription : self.torrentInfo.aniDB;
	static NSString* header													= @"<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> </head><body>";
	static NSString* footer													= @"</body></html>";
	content																	= [header stringByAppendingFormat:@"%@%@", content ? content : @"", footer];
	[self.webView loadHTMLString:content baseURL:[NSURL URLWithString:[[NexusClient sharedClient] baseURLString]]];
}

//
// refresh torrent details
//
- (void)refreshTorrentDetails:(id)sender
{
	id getTorrentDetailsFinished = ^void(GetTorrentDetailsRequest* httpRequest)
	{
		[ActivityView removeView];
		if(!httpRequest.error && httpRequest.response.statusCode == 200)
		{
			self.torrentInfo.torrentDescription								= httpRequest.torrentInfo.torrentDescription;
			self.torrentInfo.aniDB											= httpRequest.torrentInfo.aniDB;
			[self setupWebViewContent];
		}
		else
		{
			NSString* message												= [httpRequest errorDescription];
			NSString* title													= NSLocalizedStringFromTable(@"Get Torrent Details Failed", @"TorrentDescAniDBViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", nil)];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting Torrent Details", @"TorrentDescAniDBViewController", nil) networking:YES];
	[[NexusClient sharedClient] getTorrentDetails:self.torrentInfo.torrentId callback:getTorrentDetailsFinished];
}

//
// did load
//
- (void)viewDidLoad
{
	[super viewDidLoad];
	
	//
	// create web view
	//
	self.view.backgroundColor												= [UIColor whiteColor];
	self.webView															= [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
	self.webView.autoresizingMask											= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	self.webView.scalesPageToFit											= YES;
	[self.view addSubview:self.webView];
	if(self.showDescription)
		self.title															= NSLocalizedStringFromTable(@"Description", @"TorrentDescAniDBViewController", nil);
	else
		self.title															= NSLocalizedStringFromTable(@"AniDB", @"TorrentDescAniDBViewController", nil);
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTorrentDetails:)] autorelease];
	if(self.torrentInfo.torrentDescription)
		[self setupWebViewContent];
	else
		[self refreshTorrentDetails:nil];
}

@end
